Thread: error handling[c]

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    53

    error handling[c]

    hi.
    i am learning about error handling as of now and i have a simple question.
    when consider this program i made to test the error messages , what error should be used for the initial if no arguments are supplied to be correct. i did find the open groups definitions and read them all and i am trying them one by one this way but i dont seem to find the one that should be correct used in this context.
    thanks.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    int main(int argc, char** argv)
    {
    	int i;
    	/* if no argcs we get an error ?  */
    	if( argc == 1)
    	{
    		fprintf(stderr,"%s %s\n",argv[0],strerror(EACCES));
    		    	 exit(1);
    	}
    	
    
    	   for(i=1;i < argc; i++)
    	   if(strcmp(argv[i],"--version") == 0)
    	            printf(" version 0.0.1\n");
    	  
    	        else if(strcmp(argv[i],"--error1")==0)
    	           fprintf(stderr," EACCES:%s\n",strerror(EACCES));
    			   
    		else if(strcmp(argv[i],"--error2")==0)
    	           fprintf(stderr,"EADDRINUSE:%s\n",strerror(EADDRINUSE));
    		
    		else if(strcmp(argv[i],"--error3") == 0)	   
    	            fprintf(stderr,"E2BIG:%s\n",strerror(E2BIG));
    		
    		else if(strcmp(argv[i],"--error4")==0)		
    	            fprintf(stdout,"EADDRNOTAVAIL:%s\n",strerror(EADDRNOTAVAIL));
    	    
    		else if(strcmp(argv[i],"--error5")==0)        
    	            printf("EAGAIN:%s\n",strerror(EAGAIN));
    	
    	return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cmay View Post
    hi.
    i am learning about error handling as of now and i have a simple question.
    when consider this program i made to test the error messages , what error should be used for the initial if no arguments are supplied to be correct.
    Just make up something yourself. The normal way to deal with "wrong number of arguments" is a "usage" message customized for the program, eg:
    Code:
    Usage: myprog [-x] [-d] filename
    To represent how the program is suppose to be called. You can then use that same usage message for any problem with the arguments.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    thanks.
    i sometimes use the get opt function so i might as well just use the same message as it gives if an invalid argument is been giving. i did however think there was a standard error code among all those others.

    thanks for the time.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cmay View Post
    thanks.
    i sometimes use the get opt function so i might as well just use the same message as it gives if an invalid argument is been giving. i did however think there was a standard error code among all those others.

    thanks for the time.
    The standard error code for invalid arguments is EINVAL, but the resulting error message is not very informative. Better to report something more specific yourself.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Quote Originally Posted by brewbuck View Post
    The standard error code for invalid arguments is EINVAL, but the resulting error message is not very informative. Better to report something more specific yourself.
    thanks.
    i found all the error codes in linux using cat command and i figure there must be almost identical error codes for solaris and other *nix systems as the error codes should be same as in the open groups specifications.

    so i just wrote a small sample i was going to post wiht the message that i had found something out. i assume the eagain is ok to use in this context .

    thanks for the time.

    Code:
    /* i was checking for error codes using cat #include <asm-generic/errno-base.h> */
    
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    void usage();
    
    int main(int argc, char** argv)
    {
    	    if(argc == 1)
    	    {
    	        usage(argv[0]);
    		    exit(11); 
    		     /* #define	EAGAIN		11	 Try again */
    	    }
    	
    	    if((argv[1][0] == '-' && argv[1][1] == 'h') == 0 )
    	    {
    		
    	    
    	 			      fprintf(stdout,"%s %s\n",argv[0],strerror(EINVAL));
    	                  /* #define	EINVAL		22	 Invalid argument */
    	    }
    		else{
    			
    			   fprintf(stdout,"hello world\n");
    		    }
    		 
    			
    	return 0;
    }
    
    void usage(const char *progname)
    {
    	fprintf(stdout,"%s usage: [option]   \n"
    	                          "--version   version \n"
    				   "--help     this help\n"
    				  "COPYRIGHT author \n"
    				  "LICENSE	  BSD\n",progname);
    }

Popular pages Recent additions subscribe to a feed